<?php

class Category extends Tut_Model {

    public $id;
    public $name;
    public $parent_id;
    public $is_actived;
    public $description;
    public $date;
    public $last_update;

    public function __construct() {
        parent::__construct();
        $this->_load();
    }

    public function add(array $aCategory) {
        if (empty($aCategory)) {
            return false;
        }

        $aCategory = $this->_prepareArr($aCategory);
        if ((bool) $this->db->insert('product_category', $aCategory)) {
            return $aCategory;
        }

        return false;
    }

    public function edit(array $aProduct) {
        if (empty($aProduct)) {
            return false;
        }

        //Get id product because after prepareArr, it's going to unset.
        $iId = $aProduct['id'];
        $aProduct = $this->_prepareArr($aProduct, true);
        if ((bool) $this->db->update('product', $aProduct, array('id' => $iId))) {
            return $aProduct;
        }

        return false;
    }

    public function save(array $aCategory) {
        if (empty($aCategory)) {
            return false;
        }
        
        $iId = $aCategory['id'];
        unset($aCategory['id']);
        
        if(empty($iId)){
            $aCategory = $this->_prepareArr($aCategory);
            if ((bool) $this->db->insert('product_category', $aCategory)) {
                return $aCategory;
            }
            return false;
        }
        
        $aCategory = $this->_prepareArr($aCategory, true);
        if ((bool) $this->db->update('product_category', $aCategory, array('id' => $iId))) {
            return $aCategory;
        }
    }

    public function delete($iId) {
        if (empty($iId)) {
            return false;
        }
        
        $this->db->where('id', $iId);
        $this->db->update('product_category', array('is_deleted' => 1));

        if ($this->db->affected_rows() > 0) {
            return true;
        }

        return false;
    }

    public function getById($iId = 0, $bIsCheckActive = true) {
        if (empty($iId)) {
            return false;
        }

        $this->db->select('*')
                ->from('product_category')
                ->where('id', $iId)
                ->where(array('id' => $iId, 'is_deleted' => 0));

        if ($bIsCheckActive) {
            $this->db->where('is_actived', ACTIVE);
        }

        $category = $this->db->get();

        if ($category->num_rows() > 0) {
            $category = $category->row();
            $category->date = date('Y-m-d', $category->date);
            $category->last_update = date('Y-m-d', $category->last_update);

            return $category;
        }

        return false;
    }

    private function _prepareArr(array $aArr = null, $isEdit = false) {
        if (empty($aArr)) {
            return false;
        }

        if (empty($aArr['parent_id'])) {
            $aArr['parent_id'] = 0;
        }
        
        $aArr['is_actived'] = $aArr['actived'];
        unset($aArr['actived']);
        $aArr['date'] = time();
        
        if($isEdit){
            unset($aArr['date']);
        }
        
        $aArr['last_update'] = time();
        return $aArr;
    }

    public function gets($isCheckActived = false) {
        $this->db->select('id, parent_id, name')
                ->from('product_category')
                ->where(array('is_deleted' => 0))
                ->order_by('parent_id', 'asc');

        if ($isCheckActived) {
            ///////////////////////////////// 1
            $this->db->where('is_actived', 1);
        }

        $oCateogry = $this->db->get();

        $aCateogry = array();
        if ($oCateogry->num_rows() > 0) {
            foreach ($oCateogry->result() as $category) {
                $aCategory[] = $category;
            }
        }

        return $aCategory;
    }

    public function hasChildren($iId, $bIsCheckActive = false){
        if (empty($iId)) {
            return false;
        }

        $this->db->select('*')
                ->from('product_category')
                ->where(array('parent_id' => $iId, 'is_deleted' => 0));

        if ($bIsCheckActive) {
            $this->db->where('is_actived', ACTIVE);
        }

        $category = $this->db->get();

        if ($category->num_rows() > 0) {
            return true;
        }

        return false;
    }
    
    public function hasProduct($iId, $bIsCheckActive = false){
        if (empty($iId)) {
            return false;
        }

        $this->db->select('*')
                ->from('product')
                ->where(array('product_category_id' => $iId, 'is_deleted' => 0));

        if ($bIsCheckActive) {
            $this->db->where('is_actived', ACTIVE);
        }

        $category = $this->db->get();

        if ($category->num_rows() > 0) {
            return true;
        }

        return false;
    }
    
    private function _load() {
        
    }

    public function getRule($iId = false) {
       $sUniqueId = empty($iId) ? '' : "-{$iId}";
        return array(
            array(
                'field' => 'category[name]',
                'label' => $this->lang->line("phrase var='general.name'"),
                ///////////////////////////////////
                'rules' => "trim|required|min_length[2]|max_length[64]|is_unique[product_category.name{$sUniqueId}]"
            ),
            array(
                'field' => 'category[description]',
                'label' => $this->lang->line("phrase var='general.description'"),
                'rules' => 'trim|required|min_length[2]'
        ));
    }

    public function getValidationError() {
        $aField = array('name', 'description');
        $aValidationError = array();

        foreach ($aField as $field) {
            $aValidationError = array_merge($aValidationError, array("{$field}" => form_error("category[{$field}]")));
        }

        return $aValidationError;
    }

}

?>